package com.yc.fruitmarket.controller;


import java.io.UnsupportedEncodingException;
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Random;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Map;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.alipay.config.AlipayConfig;
import com.yc.fruitmarket.biz.orderBiz;
import com.yc.fruitmarket.biz.impl.orderBizImpl;

@WebServlet("/orderController/*")
public class orderController extends BaseServlet {
	private orderBiz ob = new orderBizImpl();
	
	//根据年查季度
	public void getjd(HttpServletRequest req, HttpServletResponse resp) {
		String year = req.getParameter("year");
		List<Map<String, String>> list = ob.getjd(year);
		this.send(resp, list);
	}
	
	//获取年
	public void getyear(HttpServletRequest req, HttpServletResponse resp) {
		List<Map<String, String>> list = ob.getyear();
		this.send(resp, list);
	}
	
	//获取12个月的销售额
	public void getmonth(HttpServletRequest req, HttpServletResponse resp) {
		String year = req.getParameter("year");
		List<Map<String, String>> list = ob.getmonth(year);
		this.send(resp, list);
	}
	
	//查询数据
	public void getdataL(HttpServletRequest req, HttpServletResponse resp) {
		String data = ob.getdataL();
		this.send(resp, data);
	}
	
	//查一周营业额
	public void getweekordernum(HttpServletRequest req, HttpServletResponse resp) {
		List<Map<String, String>> list = ob.getweekordernum();
		this.send(resp, list);
	}
	
	//查询订单状态总数
	public void getordernumL(HttpServletRequest req, HttpServletResponse resp) {
		List<Map<String, String>> list = ob.getordernumL();
		this.send(resp, list);
	}
	
	//统计今日下单订单
	public void sumorderL(HttpServletRequest req, HttpServletResponse resp) {
        int sum = ob.sumorder();
        this.send(resp, sum);
	}
	
	//查询订单数据
	public void openL(HttpServletRequest req, HttpServletResponse resp) {
		String oid = req.getParameter("oid");
		List<Map<String, String>> list = ob.openL(oid);
		this.send(resp, list);
	}
	
	//退款
	public void refund(HttpServletRequest req, HttpServletResponse resp) {
		String oid = req.getParameter("oid");
		int result = ob.refund(oid);
		this.send(resp, result);
	}
	
	//已完成订单
	public void overorderL(HttpServletRequest req, HttpServletResponse resp) {
		List<Map<String,String>> list = ob.overorderL();
		this.send(resp, list);
	}
	
	//未付款
	public void waitPay(HttpServletRequest req, HttpServletResponse resp) {
		String uid = req.getParameter("uid");
		String cids = req.getParameter("cids");
		String countmoneys = req.getParameter("countmoneys");
		String num = req.getParameter("num");
		String aid = req.getParameter("aid");
		String did = req.getParameter("did");
		int reslut = ob.waitPay(uid, cids, countmoneys, num, aid, did);
		this.send(resp, reslut);
	}
	
	//取消订单
	public void removebyoidL(HttpServletRequest req, HttpServletResponse resp) {
		String oid = req.getParameter("oid");
		int result = ob.removebyoidL(oid);
		this.send(resp, result);
	}
	
	public void pay(HttpServletRequest req, HttpServletResponse resp) {
		try {
			//获得初始化的AlipayClient
			AlipayClient alipayClient = new DefaultAlipayClient(AlipayConfig.gatewayUrl, AlipayConfig.app_id, AlipayConfig.merchant_private_key, "json", AlipayConfig.charset, AlipayConfig.alipay_public_key, AlipayConfig.sign_type);
			
			//设置请求参数
			AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
			alipayRequest.setReturnUrl(AlipayConfig.return_url);
			alipayRequest.setNotifyUrl(AlipayConfig.notify_url);
			
//			String oid = req.getParameter("oid");
			//商户订单号，商户网站订单系统中唯一订单号，必填
//			Random r = new Random();
//			int ono = r.nextInt(900000000)+100000000;
			String out_trade_no = new String(req.getParameter("out_trade_no").getBytes("ISO-8859-1"),"UTF-8");
			System.out.println(out_trade_no+"out_trade_no");
			//String uid = new String(req.getParameter("uid").getBytes("ISO-8859-1"),"UTF-8");
			//String cids = new String(req.getParameter("cids").getBytes("ISO-8859-1"),"UTF-8");
			//System.out.println(uid+"uid1111111111");
			//System.out.println(cids+"cids22222222222222");
			//付款金额，必填
			String total_amount = new String(req.getParameter("countmoneys").getBytes("ISO-8859-1"),"UTF-8");
			//订单名称，必填
			String subject = new String(new String("果蔬商城").getBytes("ISO-8859-1"),"UTF-8");
			//商品描述，可空
			String body = new String(new String("好吃不贵").getBytes("ISO-8859-1"),"UTF-8");
			
			alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
					+ "\"total_amount\":\""+ total_amount +"\"," 
					+ "\"subject\":\""+ subject +"\"," 
					+ "\"body\":\""+ body +"\"," 
					+ "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");
			
			//若想给BizContent增加其他可选请求参数，以增加自定义超时时间参数timeout_express来举例说明
			//alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\"," 
			//		+ "\"total_amount\":\""+ total_amount +"\"," 
			//		+ "\"subject\":\""+ subject +"\"," 
			//		+ "\"body\":\""+ body +"\"," 
			//		+ "\"timeout_express\":\"10m\"," 
			//		+ "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");
			//请求参数可查阅【电脑网站支付的API文档-alipay.trade.page.pay-请求参数】章节
			
			//请求
			String result = alipayClient.pageExecute(alipayRequest).getBody();
			
			//输出
			this.send(resp, result);
			
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (AlipayApiException e) {
			e.printStackTrace();
		}
	}
	
	//发货
	public void shipments(HttpServletRequest req, HttpServletResponse resp) {
		String oid = req.getParameter("oid");
		int Result = ob.shipments(oid);
		this.send(resp, Result);
	}
	
	//订单号查询全部商品
	public void ordershowproductL(HttpServletRequest req, HttpServletResponse resp) {
		String oid = req.getParameter("oid");
		List<Map<String, String>> list = ob.ordershowproductL(oid);
		this.send(resp, list);
	}
	
	//获取订单信息
	public void getordersL(HttpServletRequest req, HttpServletResponse resp) {
		String status = req.getParameter("status");
		List<Map<String, String>> list = ob.getordersL(status);
		this.send(resp, list);
	}
	
	//4个相同查询
	public void byselectL(HttpServletRequest req, HttpServletResponse resp) {
		String status = req.getParameter("status");
		String oid = req.getParameter("oid");
		String uname = req.getParameter("uname");
		String pname = req.getParameter("pname");
		List<Map<String, String>> list = ob.byselect(status, oid, uname, pname);
		this.send(resp, list);
	}
	
	//分页
	public void pageL(HttpServletRequest req, HttpServletResponse resp) {
		String status = req.getParameter("status");
		String oid = req.getParameter("oid");
		String uname = req.getParameter("uname");
		String pname = req.getParameter("pname");
		String start = req.getParameter("start");
		String end = req.getParameter("end");
		int pagenum = Integer.parseInt(req.getParameter("pagenum"))-1;
		List<Map<String, String>> list = ob.pageL(oid, uname, pname, status, pagenum, start, end);
		this.send(resp, list);
	}
	
	public void findOrder(HttpServletRequest req, HttpServletResponse resp) {
		String uid = req.getParameter("uid");
		String status = req.getParameter("status");
		System.out.println("uid"+uid);
		System.out.println("status"+status);
		
		if("".equals(uid)||"".equals(status)) {
			return;
		}else {
			List<Map<String, String>> list = ob.findOrder(uid, status);
			List<Map<String, String>> list1 = null;
			List<List<Map<String,String>>> arr1 = new ArrayList<>();
			int[] arr = new int[list.size()];
			for(int i = 0; i < list.size(); i++) {
				arr[i] = Integer.parseInt(list.get(i).get("oid"));
				System.out.println(arr[i]);
			}
			for(int i = 0; i < arr.length; i++) {
				int temp = arr[i];
				String temp1 = String.valueOf(temp);
				list1 = ob.findDetailOrder(uid, temp1, status);
				arr1.add(list1);
				System.out.println(list1);
			}
			System.out.println(arr1);
			this.send(resp, arr1);
		}
	}
	//确认收货地址
	public void toRecv(HttpServletRequest req, HttpServletResponse resp) {
		String oid = req.getParameter("oid");
		if("".equals(oid)) {
			this.send(resp, -1);
			return;
		}else {
			int reslut = ob.toRecv(oid);
			if(reslut > 0) {
				this.send(resp, reslut);
			}else {
				this.send(resp, -1);
			}
		}
	}
	//详情商品信息
	public void showOrder(HttpServletRequest req, HttpServletResponse resp) {
		String oid = req.getParameter("oid");
		if("".equals(oid)) {
			return;
		}else {
			List<Map<String, String>> list = ob.showOrder(oid);
			if(list.size() > 0) {
				this.send(resp, list);
			}else {
				return;
			}
		}
	}
	//用户执行可退货操作，将状态改6
	public void toTuiHuo(HttpServletRequest req, HttpServletResponse resp) {
		String oid = req.getParameter("oid");
		String uid = req.getParameter("uid");
		String reason = req.getParameter("reason");
		if("".equals(reason)||"".equals(uid)||"".equals(oid)) {
			this.send(resp, 0);
			return;
		}else {
			int result = ob.toTuiHuo(oid, uid, reason);
			if(result > 0) {
				this.send(resp, result);
			}else {
				this.send(resp, -1);
			}
		}
	}
	//修改用户管理员钱
	public void updatebalance(HttpServletRequest req, HttpServletResponse resp) {
		String oid = req.getParameter("oid");
		String uid = req.getParameter("uid");
		String countmoneys = req.getParameter("countmoneys");
		ob.updatebalance(oid, uid, countmoneys);
		this.send(resp, 1);
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
}
